<html>
<head><meta charset="utf-8"><title>UB by putting raw ptr in vector? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html">UB by putting raw ptr in vector?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="210624162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210624162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210624162">(Sep 19 2020 at 16:51)</a>:</h4>
<p>Hi I'm writing a VM and I ran miri with some code examples to check if I was causing UB, and apparently I do:</p>
<div class="codehilite"><pre><span></span><code>trying to reborrow for SharedReadOnly, but parent tag &lt;untagged&gt; does not have an appropriate item in the borrow stack
</code></pre></div>


<p>This error appears exactly on this line: <a href="https://github.com/christianpoveda/pijama/blob/master/pijama_runtime/src/instruction.rs#L462">https://github.com/christianpoveda/pijama/blob/master/pijama_runtime/src/instruction.rs#L462</a></p>
<p>What's happening is that when a new closure (my lang's closure, not a rust closure) is created I store it in the heap and take a <code>*const</code> pointer to it. That pointer is pushed into the stack and it's used when the closure is called. Given that my stack only holds <code>i64</code> I had to do some casts to make it fit (this is running on a 64-bit machine so there should be no problem with that I hope). Then when I read that pointer I get the miri error I pasted above.</p>
<p>Is this actually UB? </p>
<p>cc <span class="user-mention" data-user-id="120791">@RalfJ</span></p>



<a name="210657701"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210657701" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210657701">(Sep 20 2020 at 09:23)</a>:</h4>
<p>Does it work if you use <code>.as_ptr()</code> instead of <code>.deref()</code> at <a href="https://github.com/christianpoveda/pijama/blob/cf22ce07821a87abb607c30e12d76da5d4c3a120/pijama_runtime/src/bytecode.rs#L61">https://github.com/christianpoveda/pijama/blob/cf22ce07821a87abb607c30e12d76da5d4c3a120/pijama_runtime/src/bytecode.rs#L61</a></p>
<p>Edit: found other raw pointers in your code</p>



<a name="210658780"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210658780" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210658780">(Sep 20 2020 at 09:56)</a>:</h4>
<p><span class="user-mention" data-user-id="132916">@Christian Poveda</span> I'm afraid there's just too much code there to really tell. :/ I cannot debug this just be browsing the code. And since it's a raw pointer that got invalidated, it is also not clear what the other, conflicting access is...</p>



<a name="210658794"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210658794" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210658794">(Sep 20 2020 at 09:57)</a>:</h4>
<blockquote>
<p>This error appears exactly on this line: <a href="https://github.com/christianpoveda/pijama/blob/master/pijama_runtime/src/instruction.rs#L462">https://github.com/christianpoveda/pijama/blob/master/pijama_runtime/src/instruction.rs#L462</a></p>
</blockquote>
<p>that line doesnt even do anything with any pointer though? could you paste the full error?</p>



<a name="210658848"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210658848" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210658848">(Sep 20 2020 at 09:58)</a>:</h4>
<blockquote>
<p>What's happening is that when a new closure (my lang's closure, not a rust closure) is created I store it in the heap and take a *const pointer to it. That pointer is pushed into the stack and it's used when the closure is called. Given that my stack only holds i64 I had to do some casts to make it fit (this is running on a 64-bit machine so there should be no problem with that I hope). Then when I read that pointer I get the miri error I pasted above.</p>
</blockquote>
<p>the error indicates that <em>after</em> the <code>*const</code> got created, you are creating or passing around or otherwise handling a mutable reference to that same memory. doing so invalidates raw pointers that were created before (except if the reference is itself created from that raw pointer).</p>



<a name="210670107"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210670107" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210670107">(Sep 20 2020 at 14:51)</a>:</h4>
<p><a href="https://github.com/christianpoveda/pijama/blob/aed83e0d84b92e7537b626cad8de7cc5c8283d8a/pijama_runtime/src/instruction.rs#L525">https://github.com/christianpoveda/pijama/blob/aed83e0d84b92e7537b626cad8de7cc5c8283d8a/pijama_runtime/src/instruction.rs#L525</a></p>



<a name="210670170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210670170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210670170">(Sep 20 2020 at 14:52)</a>:</h4>
<p>ah, that line makes more sense :D</p>



<a name="210670493"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210670493" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210670493">(Sep 20 2020 at 15:00)</a>:</h4>
<p>heh yeah I forgot to use a permalink :facepalm:</p>



<a name="210670584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210670584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210670584">(Sep 20 2020 at 15:02)</a>:</h4>
<p>I don't know if this would help but each <code>Closure</code> is stored in an <code>UnsafeCell</code>. Those cells are stored in a linked list to guarantee that they won't get dropped or moved from their original location.</p>



<a name="210670647"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210670647" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210670647">(Sep 20 2020 at 15:04)</a>:</h4>
<p>Here's where each raw ptr is created: <a href="https://github.com/christianpoveda/pijama/blob/aed83e0d84b92e7537b626cad8de7cc5c8283d8a/pijama_runtime/src/heap.rs#L18">https://github.com/christianpoveda/pijama/blob/aed83e0d84b92e7537b626cad8de7cc5c8283d8a/pijama_runtime/src/heap.rs#L18</a></p>



<a name="210670657"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210670657" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210670657">(Sep 20 2020 at 15:04)</a>:</h4>
<p>Once that ptr is created it is pushed into a <code>Vec</code></p>



<a name="210670810"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210670810" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210670810">(Sep 20 2020 at 15:08)</a>:</h4>
<p>what surprises me is that nothing else is accessing this value through this pointer nor being modified between the ptr creation and when the ptr is popped from the stack.</p>



<a name="210670813"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210670813" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210670813">(Sep 20 2020 at 15:08)</a>:</h4>
<p>there's another possible cause for this error, which is that the pointer never was valid to begin with</p>



<a name="210670832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210670832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210670832">(Sep 20 2020 at 15:09)</a>:</h4>
<p>when you turn a reference <code>&amp;T</code> to a <code>*const T</code>, the resulting rwa ptr must only be used for the memory covered by that <code>T</code></p>



<a name="210670847"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210670847" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210670847">(Sep 20 2020 at 15:09)</a>:</h4>
<p>so e.g. when you are in a slice and turn a ref to the first element into a raw pointer, then the raw pointer is <em>for the first element only</em></p>



<a name="210670899"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210670899" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210670899">(Sep 20 2020 at 15:10)</a>:</h4>
<p>ohh, the whole list is behind a <code>UnsafeCell</code>, could it be that this error happens because I'm getting a pointer to each closure through a raw pointer?</p>



<a name="210670911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210670911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210670911">(Sep 20 2020 at 15:10)</a>:</h4>
<p>(re: the question in the thread title, the vector has nothing to do with it. it doesnt matter where you put the raw ptr. it only matters how you create it, and which other things access the memory it points to.)</p>



<a name="210670916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210670916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210670916">(Sep 20 2020 at 15:10)</a>:</h4>
<p>ok I get it</p>



<a name="210670928"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210670928" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210670928">(Sep 20 2020 at 15:11)</a>:</h4>
<p><span class="user-mention silent" data-user-id="132916">Christian Poveda</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F/near/210670899">said</a>:</p>
<blockquote>
<p>ohh, the whole list is behind a <code>UnsafeCell</code>, could it be that this error happens because I'm getting a pointer to each closure through a raw pointer?</p>
</blockquote>
<p>no idea, I dont know how your list works. <code>UnsafeCell</code> itself makes no difference at all here.</p>



<a name="210670934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210670934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210670934">(Sep 20 2020 at 15:11)</a>:</h4>
<p>the only thing <em>that</em> lets you do is mutate things that would usually be immutable</p>



<a name="210670944"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210670944" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210670944">(Sep 20 2020 at 15:11)</a>:</h4>
<p>yes, so the list is behind an unsafecell itself, so the only way to store something in it is to use a raw pointer</p>



<a name="210671003"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210671003" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210671003">(Sep 20 2020 at 15:12)</a>:</h4>
<p>which means that the pointer that's causing the error is created by accessing a raw pointer to the whole list. That sounds like what you said about "only using it for the memory covered by that <code>T</code>"</p>



<a name="210671015"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210671015" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210671015">(Sep 20 2020 at 15:13)</a>:</h4>
<p>or am I misunderstanding this?</p>



<a name="210671153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210671153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210671153">(Sep 20 2020 at 15:17)</a>:</h4>
<p><span class="user-mention silent" data-user-id="132916">Christian Poveda</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F/near/210670944">said</a>:</p>
<blockquote>
<p>yes, so the list is behind an unsafecell itself, so the only way to store something in it is to use a raw pointer</p>
</blockquote>
<p>these two things seem to have nothing to do with each other?</p>



<a name="210671164"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210671164" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210671164">(Sep 20 2020 at 15:17)</a>:</h4>
<p><span class="user-mention silent" data-user-id="132916">Christian Poveda</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F/near/210671015">said</a>:</p>
<blockquote>
<p>or am I misunderstanding this?</p>
</blockquote>
<p>dunno, I dont understand what you are saying.^^<br>
see <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/134">https://github.com/rust-lang/unsafe-code-guidelines/issues/134</a> for more on this behavior of raw pointers.</p>



<a name="210671213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210671213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210671213">(Sep 20 2020 at 15:18)</a>:</h4>
<p>I think probably the only way for me to say more is if you manage to reproduce the problem in a self-contained minimal example on the playground, so I can actually follow the code.</p>



<a name="210671239"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210671239" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210671239">(Sep 20 2020 at 15:19)</a>:</h4>
<p>another wild guess: you are doing <code>&amp;mut *self.nodes.get()</code></p>



<a name="210671242"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210671242" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210671242">(Sep 20 2020 at 15:20)</a>:</h4>
<p>you claimed earlier you wouldnt create references to the closures but this is exactly what that line does!</p>



<a name="210671280"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210671280" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210671280">(Sep 20 2020 at 15:20)</a>:</h4>
<p>the <code>&amp;mut</code> this creates points to the full node, including its closure field</p>



<a name="210671285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210671285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210671285">(Sep 20 2020 at 15:20)</a>:</h4>
<p>so you are, at this point, asserting that you have a unique pointer to that closure</p>



<a name="210672908"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210672908" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210672908">(Sep 20 2020 at 16:06)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F/near/210671239">said</a>:</p>
<blockquote>
<p>another wild guess: you are doing <code>&amp;mut *self.nodes.get()</code></p>
</blockquote>
<p>yes</p>



<a name="210672909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210672909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210672909">(Sep 20 2020 at 16:06)</a>:</h4>
<p>I think that's the issue after what you said</p>



<a name="210672910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210672910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210672910">(Sep 20 2020 at 16:06)</a>:</h4>
<p><a href="https://play.integer32.com/?version=stable&amp;mode=release&amp;edition=2018&amp;gist=8b483572e83a213081acbd2bf6823582">https://play.integer32.com/?version=stable&amp;mode=release&amp;edition=2018&amp;gist=8b483572e83a213081acbd2bf6823582</a></p>



<a name="210672944"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210672944" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210672944">(Sep 20 2020 at 16:07)</a>:</h4>
<p>so basically everytime I'm inserting something new, I'm invalidating all the other pointers I have anyway.</p>



<a name="210672971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB%20by%20putting%20raw%20ptr%20in%20vector%3F/near/210672971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/UB.20by.20putting.20raw.20ptr.20in.20vector.3F.html#210672971">(Sep 20 2020 at 16:08)</a>:</h4>
<p>in the playground, if you remove the second insert, there's no issue.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>